↜ Back to index Introduction to Numerical Analysis 1

Part a—Lecture 1

We start with a review of what you learned in your first year classes 情報・計算科学基礎 and 計算科学.

Fortran knowledge (Fortran知識)

You need a working Fortran compiler: gfortran

From the class Computational science (計算科学) you should already know most of the Fortran language that we will be using:

I prefer using the free-form Fortran style that does not require 6 spaces at the beginning of each line. The file extension (拡張子) must be .f90.

For details see Fortran cheatsheet.

Why do we need to compile Fortran code?

Let us consider the program that computes a factorial (階乗) of an input number.

! Program that computes a factorial
implicit none
integer i, n, f

print *, "Enter n"
read *, n

f = 1

do i = 1,n
    f = f * i
end do

print *, "Factorial of ", n, " = ", f

end

We can only run a Fortran program after we compile it. In the terminal (Cygwin for example):

$ gfortran fact.f90
$ ./a.exe 
 Enter n
5
 Factorial of            5  =          120

(Advanced) What is in the file a.exe?

Let us try to print it out:

$ cat a.exe

You will see a lot of strange characters in the output. The file a.exe is a binary file that does not contain text but contains a sequence of numbers that represent the instructions for your computer, specifically the CPU (central processing unit). This is a chip that reads a sequence of simple instructions from the computer memory and performs them one at a time. Some of these instructions read data from memory, some do arithmetic operations on the data and some print stuff to the screen.

We can print out the instructions in a.exe in a readable form using objdump program:

$ objdump -d a.exe


a.exe:     file format elf64-x86-64


Disassembly of section .init:

0000000000001000 <_init>:
    1000:   f3 0f 1e fa             endbr64 
    1004:   48 83 ec 08             sub    $0x8,%rsp
    1008:   48 8b 05 c9 2f 00 00    mov    0x2fc9(%rip),%rax        # 3fd8 <__gmon_start__>
    100f:   48 85 c0                test   %rax,%rax
    1012:   74 02                   je     1016 <_init+0x16>
    1014:   ff d0                   callq  *%rax
    1016:   48 83 c4 08             add    $0x8,%rsp
    101a:   c3                      retq   

...

You can see the instructions for subtraction (sub), addition (add), jumps to other instructions (je: jump if equal) etc. This is what your computer sees when you run the compiled Fortran program.

Exercises (練習問題)

Here are a few exercises for today to review your Fortran knowledge.

Exercise 1. Running a Fortran program

Try to run the following program. Make sure to use file extension (拡張子) .f90. If you use .f, you will have to enter 6 spaces at the beginning of each line.

print *, "Hello"
end

For example, save it in a file hello.f90, and then compile and run it as:

$ gfortran hello.f90
$ ./a.exe
Hello

Text following $ are commands to be typed on the terminal command line (端末) (cygwin on Windows). Lines without $ show the output of the commands. Replace ./a.exe with ./a.out if you are on macOS or Linux.

It might be useful to change the name of the compiled program. Change the name of the executable file in the previous exercise to hello.exe.

$ gfortran hello.f90 -o hello.exe
$ ./hello.exe
Hello

Exercise 2. These are some tricky points when working with numbers in Fortran (and on a computer in general). Make sure that you understand the outputs of these programs.

  1. What does the following program print? Why?

    i = 2.5
    if (i == 2.5) then
        print *, "Equal"
    else
        print *, "Math is wrong!"
    endif
    end
  2. What does this program print? Why?

    implicit none
    real x, y, z
    
    x = 1
    y = x / 2
    z = 1 / 2
    
    print *, y
    print *, z
    end
  3. What does this program print? Why?

    implicit none
    real x, s
    x = 2
    s = x**2
    x = 3
    
    print *, s
    end

Explanation…

Exercise 3. What is wrong the following Fortran program?

print *, 'The value of cos(2pi) is', cos(2 * pi)
end

Exercise 4. Write a program that reads an integer and prints whether it is even (遇) or odd (奇).

Submit the code to Acanthus.

Exercise 5. Solving the quadratic equation

Write a program that finds the solutions x of the quadratic equation ax^2 + bx + c = 0 given a, b and c from the user. Print the solutions, if any, from smaller to larger.

Example. Suppose that the user enters 1 -3 2, the program should output something like:

x1 = 1.00000
x2 = 2.00000

Submit the code to Acanthus.

Test questions

  1. How do you compile Fortran program equation.f90?

  2. How do you run compiled Fortran program?

  3. What is the difference between Fortran programs in a file with extension .f and extension .f90?

  4. What does implicit none do?